package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增菜品分类
     *
     * @param categoryDTO
     * @return
     */
    @Transactional
    @Override
    @CacheEvict(cacheNames = "setmealCache",key = "#categoryDTO.categoryId")
    public Result addCategoryType(CategoryDTO categoryDTO) {
        if (categoryDTO.getType()==null){
            return Result.error(MessageConstant.SETMEAL_ENABLE_FAILED);
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(StatusConstant.ENABLE);
        categoryMapper.addCategoryType(category);
        return Result.success();
    }

    /**
     * 分类分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public Result page(CategoryPageQueryDTO categoryPageQueryDTO) {
        if (categoryPageQueryDTO.getPage()==null){
            categoryPageQueryDTO.setPage(1);
        }
        if (categoryPageQueryDTO.getPageSize()==null){
            categoryPageQueryDTO.setPageSize(5);
        }
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page<Category> categoryPage = categoryMapper.pageQuery(categoryPageQueryDTO);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(categoryPage.getTotal());
        pageResult.setRecords(categoryPage.getResult());
        return Result.success(pageResult);
    }

    /**
     * 按照id删除
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    @CacheEvict(cacheNames = "userCache",allEntries = true)
    public Result delete(Long id) {
        Integer count = dishMapper.countByCategory(id);
        if (count>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        count = setmealMapper.countByCategory(id);
        if (count>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.delete(id);
        return Result.success();
    }
    /**
     * 修改菜品分类
     *
     * @param categoryDTO
     * @return
     */
    @Transactional
    @Override
    @CacheEvict(cacheNames = "userCache",allEntries = true)
    public Result updateCategory(CategoryDTO categoryDTO) {
        if (categoryDTO.getId()==null){
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.updateCategory(category);
        return Result.success();
    }

    /**
     * 启用和禁用
     *
     * @param status
     * @param id
     * @return
     */
    @Transactional
    @Override
    @CacheEvict(cacheNames = "userCache",allEntries = true)
    public Result updateCategoryStatus(Integer status, Long id) {
        if (status == null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        if (id == null){
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        Category category = new Category();
        category.setStatus(status);
        category.setId(id);
        categoryMapper.updateCategory(category);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    @Cacheable(cacheNames = "userCache",key="'category'")
    public Result<List<Category>> list(Integer type) {

        List<Category> list = categoryMapper.list(type);
        return Result.success(list);
    }


}
